package com.lh.jianzhioffer.专项练习.字符串;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author: linghao
 * @since: 2023/3/22
 *
 * 给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示
 *
 * 解答：排序后相邻之间的
 */
public class 最小时间差035 {

    //["00:00","23:59","00:00"]
    public static void main(String[] args) {
        List<String> timePoints = new ArrayList<>();
////        timePoints.add("00:00");
        timePoints.add("05:31");
        timePoints.add("22:08");
        timePoints.add("00:35");
//        timePoints.add("02:01");
//        timePoints.add("03:00");
//        timePoints.add("01:01");
        System.out.println(findMinDifference(timePoints));
    }

    public static int findMinDifference(List<String> timePoints) {
        //先将时间转化为分钟总数
        Collections.sort(timePoints);
        int min = getMuties(timePoints.get(0));
        for(int i=1;i<timePoints.size();i++){
            int t1 = getMuties(timePoints.get(i));
            int t2 = getMuties(timePoints.get(i-1));
            min = Math.min(min,t1-t2);
        }
        //首位相比较
        min = Math.min(min,getMuties(timePoints.get(0)) + 1440 - getMuties(timePoints.get(timePoints.size()-1)));
        return min;
    }

    private static int getMuties(String s) {
        return (s.charAt(0)*10+s.charAt(1))*60+s.charAt(3)*10+s.charAt(4);
    }

}
